﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bxas.Utility.StringOperations
{
    public class Operations
    {
        public static string random_string(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            string value = "";

            foreach (var item in builder.ToString())
            {
                if (item == 'ı') value += "i";
                else if (item == 'İ') value += "I";
                else if (item == 'ğ') value += "g";
                else if (item == 'Ğ') value += "G";
                else if (item == 'ü') value += "u";
                else if (item == 'Ü') value += "U";
                else if (item == 'ş') value += "s";
                else if (item == 'Ş') value += "S";
                else if (item == 'ö') value += "O";
                else if (item == 'Ö') value += "O";
                else if (item == 'ç') value += "c";
                else if (item == 'Ç') value += "C";
                else value += item.ToString();
            }

            if (lowerCase)
                return value.ToLower();
            return value.ToString();
        }

        public static string replace_tr_chars_to_en(string val)
        {
            string newvalue = string.Empty;

            foreach (var item in val)
            {
                if (item == 'ı') newvalue += "i";
                else if (item == 'İ') newvalue += "I";
                else if (item == 'ğ') newvalue += "g";
                else if (item == 'Ğ') newvalue += "G";
                else if (item == 'ü') newvalue += "u";
                else if (item == 'Ü') newvalue += "U";
                else if (item == 'ş') newvalue += "s";
                else if (item == 'Ş') newvalue += "S";
                else if (item == 'ö') newvalue += "O";
                else if (item == 'Ö') newvalue += "O";
                else if (item == 'ç') newvalue += "c";
                else if (item == 'Ç') newvalue += "C";
                else newvalue += item.ToString();
            }
            return newvalue;
        }

        public static string get_google_log_script(string val)
        {
            StringBuilder googlelogScript = new StringBuilder();
            googlelogScript.AppendLine("");
            googlelogScript.AppendLine("<script type=\"text/javascript\">");
            googlelogScript.AppendLine("var _gaq = _gaq || [];");
            googlelogScript.AppendLine("_gaq.push(['_setAccount', '" + val + "']);");
            googlelogScript.AppendLine("_gaq.push(['_trackPageview']);");
            googlelogScript.AppendLine("(function() {");
            googlelogScript.AppendLine("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
            googlelogScript.AppendLine("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
            googlelogScript.AppendLine("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
            googlelogScript.AppendLine("})();");
            googlelogScript.AppendLine("</script>");
            return googlelogScript.ToString();
        }

        //public static string format_telephone(string phone)
        //{
        //    string formated = "0(";
        //    for (int i = 0; i < phone.Length; i++)
        //    {
        //        if (i == 3) formated += ") ";
        //        else if (i == 6) formated += " ";
        //        else if (i == 8) formated += " ";

        //        formated += phone[i].ToString();
        //    }
        //    return formated;
        //}

        //public static string reformat_telephone(string formated_telephone)
        //{
        //    string reformated = "";
        //    for (int i = 0; i < formated_telephone.Length; i++)
        //    {
        //        if (i != 0 && i != 1 && i != 5 && i != 6 && i != 10 && i != 13)
        //            reformated += formated_telephone[i].ToString();

        //    }
        //    return reformated;
        //}

        public static string get_unique_code_from_date(DateTime dt)
        {
            string ycode = ((char)(dt.Year - 1945)).ToString();
            string dcode = "";

            int k = dt.DayOfYear;
            for (; k > 7; )
            {
                dcode += (k % 7).ToString();
                k = k / 7;
            }
            dcode += k.ToString();
            char[] arr = dcode.ToCharArray();
            Array.Reverse(arr);
            dcode = new string(arr);

            string rcode = StringOperations.Operations.random_string(2, false);

            string c1 = ((char)(65 + dt.Hour)).ToString();
            string c2 = dt.Minute.ToString();
            if (c2.Length == 1) c2 = "0" + c2;
            string c3 = dt.Second.ToString();
            if (c3.Length == 1) c3 = "0" + c3;

            string code = ycode + dcode + "-" + c1 + c2 + c3 + rcode;
            return code;
        }
        public static string convert_telephone_to_string_only_number(string formated_telephone)
        {
            string reformated = "";
            for (int i = 0; i < formated_telephone.Length; i++)
            {
                if (formated_telephone[i].ToString() == "0"
                 || formated_telephone[i].ToString() == "1"
                 || formated_telephone[i].ToString() == "2"
                 || formated_telephone[i].ToString() == "3"
                 || formated_telephone[i].ToString() == "4"
                 || formated_telephone[i].ToString() == "5"
                 || formated_telephone[i].ToString() == "6"
                 || formated_telephone[i].ToString() == "7"
                 || formated_telephone[i].ToString() == "8"
                 || formated_telephone[i].ToString() == "9")
                    reformated += formated_telephone[i].ToString();
            }
            return reformated;
        }
    }
}
